{extend name="_container/add"}
{block name="content"}
<div class="layui-form-item">
  <label  class="layui-form-label">其他表</label>
  <div class="layui-input-inline">
    <input type="hidden" name="is_other" value="0">
    <input type="checkbox" class="layui-input" name="is_other"   id="is_other" lay-skin="switch" value="1">
  </div>
</div>

<div hidden id="wrapper">
  <div class="layui-form-item">
    <label  class="layui-form-label">ip</label>
    <div class="layui-input-inline">
      <input type="text" class="layui-input" name="ip"   id="ip">
    </div>
  </div>
  <div class="layui-form-item">
    <label  class="layui-form-label">端口号</label>
    <div class="layui-input-inline">
      <input type="text" class="layui-input" name="port"  value="3306" id="port">
    </div>
  </div>
  <div class="layui-form-item">
    <label  class="layui-form-label">数据库</label>
    <div class="layui-input-inline">
      <input type="text" class="layui-input" name="name"  value="{$database}" id="name">
    </div>
  </div>
  <div class="layui-form-item">
    <label  class="layui-form-label">账号</label>
    <div class="layui-input-inline">
      <input type="text" class="layui-input" name="user"   id="user">
    </div>
  </div>
  <div class="layui-form-item">

    <label  class="layui-form-label">密码</label>
    <div class="layui-input-inline">
      <input type="text" class="layui-input" name="pwd"   id="pwd">

    </div>
  </div>
</div>


<div class="layui-form-item">

  <label  class="layui-form-label">项目名称</label>
  <div class="layui-input-inline">
    <input type="text" class="layui-input" name="filename"  required id="filename">
    <span class="layui-btn layui-btn-xs" id="check">check</span>
  </div>
</div>


<div class="layui-form-item">

  <label  class="layui-form-label">数据表</label>
  <div class="layui-input-blockcom" id="dbs">
  </div>
</div>




<div>

</div>

{/block}
{block name="js"}
<script>
  function save_form3(obj){
    // save_form2(obj);
    return true;

  }
</script>
<script>
  let database = "{$database}";
  layui.use(
          ['form'],
          ()=>{
            let form = layui.form;
            form.on('switch',(data)=>{
              if(data.elem.name === 'is_other'){
                if(data.elem.checked){
                  $('#is_other').val(1);
                  $('#wrapper').show();
                  $('#name').val('');
                }else{
                  $('#is_other').val(0);
                  $('#wrapper').hide();
                  $('#name').val(database);
                }
              }
            })
            $('#check').click(
                    ()=>{
                      let name = $('#name').val();
                      let ip = $('#ip').val();
                      let pwd = $('#pwd').val();
                      let user = $('#user').val();
                      let is_other = $('#is_other').val();
                      if((ip && name && pwd && user && is_other) || !is_other){
                        $.post(
                                "{:url('getTables')}",
                                {
                                  ip, name, pwd, user, is_other
                                },
                                (data)=>{
                                  if(data.code === 1){
                                    let msg = data.result;
                                    let htmlCode = '';
                                    for(let i in msg){
                                      htmlCode += '  <input type="checkbox" class="layui-input" checked name="dbs[ ]" value="' + msg[i] + '" title="' + msg[i] +'" >'
                                    }
                                    console.log(htmlCode)
                                    $('#dbs').html(htmlCode);
                                    form.render('checkbox');
                                  }else{
                                    layer.msg('数据库链接失败')
                                  }
                                }
                        )
                      }else{
                        layer.msg('数据库参数不全');
                      }

                    })
          });


</script>
{/block}
{/extend}
